package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.controller.user.UserInfoController;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService systemUserService;

    //todo: 非登录的查询，不允许回显密码
    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> page = new Page<>(current,size);
        systemUserService.customPage(page,queryVo);
        return Result.ok(page);
    }


    //todo: 不要分步处理，直接多表查询即可，复用之前的sql语句即可！！
    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = systemUserService.customGetById(id);
        return Result.ok(systemUserItemVo);
    }

    /**
     *
     * 用户保存和更新 == 注册和修改密码
     *    密码需要加密处理！
     *    使用md5加密算法！
     *  1. 导入依赖 common
     *       <dependency>
     *       <groupId>commons-codec</groupId>
     *       <artifactId>commons-codec</artifactId>
     *      </dependency>
     *  2.使用md5加密算法
     * @param systemUser
     * @return
     */
    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        // md5优势：密码你自己知道！！！
        //可能传递密码 ： 保存用户（新密码） | 修改用户信息 （修改后的密码）  明文 -》 md5 -> 密文
        //可能不传递密码： 修改，不想修改密码 -》 不用加密
        if (systemUser.getPassword() != null){
            //导包 com.apache..
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        boolean saveOrUpdate = systemUserService.saveOrUpdate(systemUser);
        return saveOrUpdate?Result.ok():Result.fail();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        //select count(*) from system_user where username = 'admin' and is_deleted = 0 ;
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername, username);
        long count = systemUserService.count(lambdaQueryWrapper);
        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(SystemUser::getId, id);
        lambdaUpdateWrapper.set(SystemUser::getStatus, status);
        systemUserService.update(lambdaUpdateWrapper);
        return Result.ok();
    }
}
